//
//  MainClass.m
//  OPFramework
//
//  Created by malcom on 7/16/08.
//  Copyright 2008 malcom-mac software. All rights reserved.
//

#import "MainClass.h"
#import "OPTopic.h"
#import "OPSmartTopics.h"
#import "OPSendPost.h"
#import "OPPrivateMsg.h"
#import "OPMembersList.h"
#import "OPSearch.h"

@implementation MainClass

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
	OPAuthUser *user = [[OPAuthUser alloc] initWithUsername: @"malcom" andPassword: @"carbon"];
	//OPBoard *board = [[OPBoard alloc] initWithBoardAddress: @"http://localhost/forum" withAuth: [user autorelease]];
	OPBoard *board = [[OPBoard alloc] initBoardAndLoadAsync: NO address:@"http://localhost/forum" withAuth:[user autorelease] withDelegate: self];

	board._config_workAsAsync = NO;
	
	NSLog(@"%@",[user description]);
	
	NSLog(@"%f",[[NSDate dateWithNaturalLanguageString: @"2008/05/24 03:41 AM"] timeIntervalSince1970]);
	
	OPForum *forum = [board.obj_boardStructure getForumFromID: [NSNumber numberWithInt: 144]];
	
	OPSearch *search = [[OPSearch alloc] initToSearch: OPSearch_WhatResult_Posts
												keywords: @"ciao" 
												inBoard:board 
												specificTopicID: nil 
												limitToForums: nil 
												searchWhere:OPSearch_Where_All 
												orderBy: OPSearch_OrderRes_Time 
												desc: YES 
												howShow:200 
											limitByDays: 30];
	NSLog(@"%@",[[search getResultsPage: [NSNumber numberWithInt: 0]] description]);
	
//	OPMembersList *list = [[OPMembersList alloc] initToListUsersStartsWithChar: 'a' fromBoard: board];
//	OPMembersList *list = [[OPMembersList alloc] initToSearchCommonPropertiesContains: @"malc*" fromBoard: board];
//	NSLog(@"%@",[list getMembersPage: [NSNumber numberWithInt: 0] usingCache: YES]);

/*	OPSmartTopics *unans = [[OPSmartTopics alloc] initWithBoard: board andListener:self];
	NSLog(@"%@",[unans getBookmarkedTopicsUsingCache: NO]);
*/
	//NSLog(@"%@",[board getBoardLeaders]);
	//NSLog(@"%@",[forum getModerators]);
	
	//NSLog(@"%@",[[user getOnlineFriends] description]);
	
	//NSLog(@"%@",[[board.obj_membersCache getMembersListPage: [NSNumber numberWithInt: 0] usingCache: YES] description]);
	
	/*NSArray *p = [user.user_personalBox getPrivateMessagesCached: YES];
	NSLog(@"%@",[p description]);
	OPPrivateMsg *msg = [p objectAtIndex: 0];
	NSLog(@"sending reply... %d",[msg replyWithMessage: @"merdoso caccoloso" withOptions:nil]);
	*/
	
	/*
	OPUser *_user = [[OPUser alloc] initUserID: [NSNumber numberWithInt: 4] fromBoard: board];
	[user.user_personalBox sendNewPrivateMessageTo: _user withSubject: @"il soggetto è..." text:@"un corbo" options:nil];
	*/
	
	//NSArray *list = [forum getTopicsAtPage: [NSNumber numberWithInt:0] cached: YES];
	//NSLog(@"%@",[list description]);
	
//	OPTopic *topic = [[OPTopic alloc] initLoadingTopicWithID: [NSNumber numberWithInt: 4782]  fromForum: forum password: @""];
//	NSLog(@"%@",[topic description]);
	/*
	OPSendPost *newPost = [[OPSendPost alloc] initNewPostWithSubject: @"nuovo del cazzo" message: @"che topic di merda :lol:" inForum: forum];
	NSLog(@"SENDING NEW TOPIC: %d",[[newPost sendMessageWithPassword: nil] intValue]);
	*/
	
//	if (list != nil) {
		
//		OPPost *post = [[OPPost alloc] initLoadingPostWithID: [NSNumber numberWithInt: 132603] fromBoard: board];
//		NSLog(@"REMOVED: %d",[post deleteFromServerAndResetPagesCache: NO withPassword: nil]);
		
		/*OPSendPost *reply = [[OPSendPost alloc] initToReplyToTopic: [list objectAtIndex:0] quoting:nil fromBoard:board];
		reply.p_messageText = [NSString stringWithString: @"che merda di roba"];
		//[reply addObjectAsListenerForMe: self];
	
		NSLog(@"Sending: %@",[reply description]);
	
		NSString *newPostID = [reply sendMessage];
		if (newPostID != nil) NSLog(@"New post has the id: %@",newPostID);
		else NSLog(@"Post failed with error %d",reply._lastErrorCode);
		 */
//	} else NSLog(@"Cannot continue to send message");
					 
	/* OPSmartTopics *unans = [[OPSmartTopics alloc] initWithBoard: board andListener:self];
	NSLog(@"%@",[unans getActiveTopicsSinceDays: [NSNumber numberWithInt: 100] atPage:[NSNumber numberWithInt: 0] cached: YES]);
	NSLog(@"%@",[unans getUnansweredTopicsSinceDays: [NSNumber numberWithInt: 100] atPage:[NSNumber numberWithInt: 0] cached: YES]);
	NSLog(@"%@",[unans getNewTopicsSinceLastLoginAtPage: [NSNumber numberWithInt: 0] cached:YES]);
	
	[unans addObjectAsListenerForMe: self];
	*/
	
	/*NSCalendarDate *start = [[NSCalendarDate alloc] initWithYear: 2008 month:1 day:1 hour:0 minute:0 second:0 timeZone:nil];
	NSCalendarDate *end = [[NSCalendarDate alloc] initWithYear: 2008 month:1 day:10 hour:0 minute:0 second:0 timeZone:nil];
	NSLog(@"%@",[unans getNewTopicsFromDate: start endDate:end atPage: [NSNumber numberWithInt: 0] cached:YES]);
*/
	
	/*NSLog(@"%@",[unans getRecentAnnouncements: [NSNumber numberWithInt: 3] cache: YES]);
	NSLog(@"%d items",unans._recentAnnouncements.p_totalElements);
	NSLog(@"%@",board.obj_membersCache);
	
	NSLog(@"%@",[board.obj_membersCache getOnlineUsersCached: YES]);
	*/
	/*NSLog(@"%@",[board.obj_membersCache getOnlineUsersCached: YES]);
	OPUser *us = [board.obj_membersCache getUserMemberFromID: [NSNumber numberWithInt: 2]];
	[us refreshCompleteProfile];
	NSLog(@"%@",[us description]);
	*/
/*	OPPost *post = [[OPPost alloc] initLoadingPostWithID: [NSNumber numberWithInt: 131881] fromForum:[board.obj_boardStructure getForumFromID: 144]];
	NSLog(@"POST: %@",[post description]);
*/
	
/*	OPPost *post = [board getPostWithID: [NSNumber numberWithInt:132527]  forceLoad: YES];
	NSLog(@"POST: %@ - located in page %d",[post description],[post getPostPageIndexInTopic]);
*/
	/*OPUser *ouser = [[OPUser alloc] initUserID: [NSNumber numberWithInt: 4] fromBoard: board];
	NSLog(@"%@",[ouser description]);
	*/
	
//	NSLog(@"%@",[board.obj_boardStructure getFlatNormalForumsList]);
	
	//NSLog(@"private msgs %d",[board.obj_authUser unreadPrivateMessages]);
	
	//NSLog(@"Sendmail: %d",[us sendEmailWithSubject: @"soggetto" andText:@"testo"]);
	
	/*OPForum *forum = [board.obj_boardStructure getForumFromID:144]; 
	
	NSArray *topics = [forum getTopicsAtPage: [NSNumber numberWithInt: 0] cached: YES];
	[forum addObjectAsListenerForMe: self];
	
	*/
	//	NSLog(@"Topic: %@ (%d posts)",[topic getPropertyForKey: OPTopic_TopicTitle],[[topic getPropertyForKey: OPTopic_TopicReplies] intValue]);
	
	//NSLog(@"%@ by %@ (%d posts)",[topic getPropertyForKey: OPTopic_TopicTitle],[topic getTopicAuthor].username,[[[topic getTopicAuthor] getPropertyForKey: OPUserProp_NumberOfPosts] intValue]);
	//for (OPTopic *cTopic in topics) {
	//	NSLog(@"%@ by %@",[cTopic getPropertyForKey: OPTopic_TopicTitle],[cTopic getPropertyForKey: OPTopic_TopicFirstPosterName]);
//	}
}

- (void) dataUpdatedFor:(OPBaseObject *) _obj atMethod:(NSString *) _method {	
	//NSLog(@"[MAINCONTROLLER] Refreshed DATA: %@",[_obj description]);

	if ([_obj isKindOfClass: [OPForum class]]) {

		NSArray *topics = [(OPForum*)_obj getTopicsAtPage: 0];

		OPTopic *topic = [topics objectAtIndex: 0];
		[topic addObjectAsListenerForMe: self];
	
		[topic getPostsInPage: 0];
		NSLog(@"[MAINCONTROLLER] Topics loaded for FORUM: %@ ",[_obj description]);

	}
	
	if ([_obj isKindOfClass: [OPTopic class]]) {
		NSLog(@"[MAINCONTROLLER] Messages loaded for TOPIC: %@ ",[_obj description]);
		
		OPPost *choosedPost = [[(OPTopic*)_obj getPostsInPage: 0] objectAtIndex:0];
		NSLog(@"[MAINCONTROLLER] First Message of the topics is: ",[choosedPost description]);
		//[choosedPost refreshData];
		NSLog(@"[MAINCONTROLLER] Message: %@",(OPPost *)choosedPost.p_message);
		
		NSLog(@"[MAINCONTROLLER] Refreshing post...");
		[choosedPost addObjectAsListenerForMe: self];
		//[choosedPost refreshData];
		NSLog(@"%@", [choosedPost.p_author description]);
	}
	
	if ([_obj isKindOfClass: [OPPost class]]) {
	//	NSLog(@"[MAINCONTROLLER] Refreshed DATA: %@",[(OPPost *)_obj getPropertyForKey: OPPost_Post_Message]);
	}
}

- (void) errorReceivedForDataUpdate:(NSNumber *) _errorCode from:(OPBaseObject *) _obj {
	NSLog(@"[ERROR] Error response: %d from %@",[_errorCode intValue],[_obj description]);
}

- (void) serverErrorForDataUpdateFrom:(OPBaseObject *) _obj error:(NSDictionary *) _dict {
	NSLog(@"[ERROR] Update error from %@: %@",_obj,_dict);
}	

- (void) opboard_init_boardNotSupported:(NSString *) _rootPath {
	
}

- (void) opboard_init_loginResult:(int) _res forBoard:(OPBoard *) _board {
	
}

- (void) opboard_init_configurationLoaded:(BOOL) _loaded forBoard:(OPBoard *) _board {
	
}

- (void) opboard_init_structureLoaded:(BOOL) _loaded forBoard:(OPBoard *) _board {
	
}

- (void) opboard_init_boardReady:(BOOL) _ready forBoard:(OPBoard *) _board {
	
}

@end
